package cn.bookingsmart.controller;

import cn.bookingsmart.common.msg.MsgDefinition;
import cn.bookingsmart.common.msg.ResultMsg;
import cn.bookingsmart.domain.User;
import cn.bookingsmart.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by sun on 2018/3/7.
 *
 * @author sunfuchang03@126.com
 * @version 1.0
 * @since 1.0
 */
@Controller
@RequestMapping("/user")
public class UserController {
    //~ Static fields/initializers =====================================================================================

    private static Logger log = LoggerFactory.getLogger(UserController.class);
    //~ Instance fields ================================================================================================
    @Autowired
    private UserService userService;
    //~ Constructors ===================================================================================================


    //~ Methods ========================================================================================================
    @RequestMapping("/save")
    @ResponseBody
    public ResultMsg saveUser(User user) {
        // 测试
        // 名称长度为6，添加正常数据
        user = new User();
        user.setName("abcdef");
        user.setAge(10);
        try {
//            userService.add(user);
//            userService.insert(user);
            userService.tryInsert(user);
            return ResultMsg.success(user);
        } catch (Exception e) {
            return ResultMsg.error(MsgDefinition.UNKOWN_ERROR.codeOf(), e.getMessage());
        }
    }

    @RequestMapping("/saveerr")
    @ResponseBody
    public ResultMsg saveErrorUser(User user) {
        // 测试
        // 名称长度为6，添加异常数据
        user = new User();
        user.setName("abcdefg123");
        user.setAge(10);
        try {
//            userService.add(user);
            userService.insert(user);
            return ResultMsg.success(user);
        } catch (Exception e) {
            return ResultMsg.error(MsgDefinition.UNKOWN_ERROR.codeOf(), e.getMessage());
        }
    }
}
